package com.qianwei.footpath.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qianwei.footpath.R;

public class ImageLoaderUtil {
    static ImageLoaderUtil instance;

    public ImageLoaderUtil() {
    }

    public static ImageLoaderUtil getInstance() {
        if (null == instance) {
            synchronized (ImageLoaderUtil.class) {
                if (null == instance) {
                    instance = new ImageLoaderUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 普通加载
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadImage(Context context, Object url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.color.color_f4f4f4)
                .error(R.color.color_f4f4f4)
                .centerInside();
        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    public void loadImage(Context context, Object url, ImageView imageView, int resourceId) {
        RequestOptions options = new RequestOptions()
                .centerInside()
                .placeholder(resourceId)
                .error(resourceId);
        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    public void loadGifImage(Context context, Object url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.color.color_eeeeee)
                .error(R.color.color_eeeeee)
                .centerInside();
        Glide.with(context).asGif().load(mergeUrl(url)).apply(options).into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadCircleImage(Context context, Object url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(Util.getImageRadiusDefault(context))
                .error(Util.getImageRadiusDefault(context))
                .fitCenter()
                .circleCrop();
        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadCircleImage(Context context, Object url, ImageView imageView, int resourceId) {
        RequestOptions options = new RequestOptions()
                .fitCenter()
                .circleCrop()
                .error(resourceId)
                .placeholder(resourceId);
        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }


    /**
     * 加载圆角图片
     *
     * @param context
     * @param url
     * @param imageView
     * @param roundingRadius 圆角度 单位dp
     */
    public void loadRoundImage(Context context, Object url, ImageView imageView, int roundingRadius) {
        int radius = Util.dip2px(roundingRadius);
        RequestOptions options = new RequestOptions()
                .placeholder(Util.getImageRadiusDefault(context, radius))
                .error(Util.getImageRadiusDefault(context, radius))
                .transform(new GlideRoundTransform(context, radius));

        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    public void loadRoundImageOrColor(Context context, Object url, ImageView imageView, int roundingRadius, int color) {
        int radius = Util.dip2px(roundingRadius);
        RequestOptions options = new RequestOptions()
                .placeholder(Util.getImageRadiusDefault(radius, context.getResources().getColor(color)))
                .error(Util.getImageRadiusDefault(radius, context.getResources().getColor(color)))
                .transform(new GlideRoundTransform(context, radius));

        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    public void loadRoundImage(Context context, Object url, ImageView imageView, int roundingRadius, int k, int g) {
        int radius = Util.dip2px(roundingRadius);
        RequestOptions options = new RequestOptions().override(Util.dip2px(k), Util.dip2px(g))
                .placeholder(Util.getImageRadiusDefault(context, radius))
                .error(Util.getImageRadiusDefault(context, radius))
                .transform(new GlideRoundTransform(context, radius));

        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    public void loadRoundImage(Context context, Object url, ImageView imageView, int roundingRadius, int resourceId) {
        int radius = Util.dip2px(roundingRadius);
        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(context, radius))
                .placeholder(resourceId)
                .error(resourceId);

        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }

    /**
     * 加载圆角图片
     *
     * @param roundingRadius 圆角度 单位dp
     *                       只保留左上角、右上角圆角
     */
    public void loadRoundImageTop(Context context, Object url, ImageView imageView, int roundingRadius) {
        int radius = Util.dip2px(roundingRadius);
        RequestOptions options = new RequestOptions()
                .placeholder(Util.getImageRadiusDefault(context, radius))
                .error(Util.getImageRadiusDefault(context, radius))
                .transform(new GlideRoundTransformHalf(context, radius));

        Glide.with(context).load(mergeUrl(url)).apply(options).into(imageView);
    }


    /**
     * 处理URL链接
     */
    public Object mergeUrl(Object url) {
        if (url == null) {
            return "";
        }
        if (url instanceof String) {
            String charSequence = (String) url;
            if (TextUtils.isEmpty(charSequence)) {
                return "";
            }
            if (charSequence.contains("http") || charSequence.contains("https") || charSequence.contains("content://")
                    || charSequence.contains("/storage/emulated") || charSequence.contains("/data/user")) {
                return url;
            } else {
                return "https://oss.aiqianwei.com/" + url;
            }

        }
        return url;
    }

}
